//
//  HotViewcontroller.m
//  YSBEmployer
//
//  Created by developer on 2021/12/05.
//  Copyright © 2021 ysb. All rights reserved.
//

#import "YSBWebViewController.h"

#import "YSBAnnouncementScrollView.h"

#import "YSBHomeController.h"

#import "YSBCompanyTableController.h"

#import "HttpClient.h"

#import "YSBMainADImageCell.h"
#import "YSBMainADImageAndTextCell.h"

#import "YSBCompanyMsgCell.h"

#import "YSBTextFieldCell.h"


#import "YSBChatRoomWebSocketManager.h"

#import "YSBMessageListHomeViewController.h"


@implementation YSBWebViewController

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    
    
}


@end



@implementation YSBMainADImageAndTextCell


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

        self.bgView.frame = CGRectMake(0, 10, frame.size.width, 330);
        self.bgView.center = self.center;
        [self addSubview:self.bgView];

        self.mainImgView.frame = CGRectMake(0, 0, frame.size.width, 160);
        [self.bgView addSubview:self.mainImgView];

        self.titleLabel.frame = CGRectMake(10, 160 + 10, frame.size.width - 20, 42);
        [self.bgView addSubview:self.titleLabel];
        
        self.contentLabel.frame = CGRectMake(10, 160 + 10 + 42 + 10, frame.size.width - 20, 40);
        [self.bgView addSubview:self.contentLabel];

        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 160 + 10 + 42 + 10 + 40 + 10, frame.size.width, 0.5)];
        lineView.backgroundColor = [UIColor grayColor];
        [self.bgView addSubview:lineView];

        self.goBtn.frame = CGRectMake(5, 160 + 10 + 42 + 10 + 40 + 10 + 10, frame.size.width - 10, 40);
        [self.bgView addSubview:self.goBtn];
        
        
    }
    return self;
}


- (UIImageView *)mainImgView{
    if (!_mainImgView) {
        _mainImgView = [[UIImageView alloc] init];
        _mainImgView.contentMode = 0;
    }
    return _mainImgView;
}

@end



@implementation YSBMainADImageCell


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.bgView.frame = CGRectMake(10, (frame.size.height - 220) * 0.5, frame.size.width - 20, 220);
        [self addSubview:self.bgView];

        self.mainImgView.frame = CGRectMake(0, 0, frame.size.width - 20, 160);
        [self.bgView addSubview:self.mainImgView];
        
        self.goBtn.frame = CGRectMake(5, 160 + 10, frame.size.width - 30, 40);
        [self.bgView addSubview:self.goBtn];
        
        
    }
    return self;
}


- (UIImageView *)mainImgView{
    if (!_mainImgView) {
        _mainImgView = [[UIImageView alloc] init];
        _mainImgView.contentMode = 0;
    }
    return _mainImgView;
}

@end



@implementation YSBAnnouncementScrollView


- (void)backGoBtnWithModel:(YSBAnnouncementModel *)model {
    
    if (self.clickBottomBtnBlock) {
        self.clickBottomBtnBlock(model);
    }
    NKAlertView *alertView = (NKAlertView *)self.superview;
    [alertView viewHidden];
    
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    [localSave setObject:[NSNumber numberWithBool:NO] forKey:@"closeHomeActivityKey"];
    [localSave synchronize];
    
}


- (void)bottomTouchTap:(UITapGestureRecognizer *)sender{
    
    
    self.closeImgView.hidden = YES;
    self.btnInfoLabel.hidden = YES;

    if (self.clickClosedBtnBlock) {
        self.clickClosedBtnBlock();
    }
    [self.main removeFromSuperview];
    NKAlertView *alertView = (NKAlertView *)self.superview;
    [alertView hide];
    
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    [localSave setObject:[NSNumber numberWithBool:YES] forKey:@"closeHomeActivityKey"];
    [localSave synchronize];
    
}



@end



@implementation YSBHomeController


- (void)loadView {
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadLogoutStatus) name:@"deleteAddressNotification" object:nil];

}



- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    CGFloat showTime = 2.5f;
    
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    NSNumber *isCloseHomeActivityNum = [localSave objectForKey:@"closeHomeActivityKey"];
    BOOL isCloseHomeActivity = isCloseHomeActivityNum.boolValue;
    if (isCloseHomeActivity == 0) {
        showTime = 0;
    }

    [self performSelector:@selector(delayShowAdMethod) withObject:nil afterDelay:showTime];
    
}

- (void)delayShowAdMethod {
    if ([YSBMSAlertManager shareManager]) {
        [[YSBMSAlertManager shareManager] showAlerts];
    }
}



@end



@implementation YSBCompanyTableController

- (void)loadView
{
    [super loadView];

    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    [localSave setObject:@"N" forKey:@"changeAddressKey"];
    [localSave synchronize];
    
}


- (void)navigationLeftAction {
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    NSString *changeAddress = [localSave objectForKey:@"changeAddressKey"];
    if ([changeAddress isEqualToString:@"Y"]) {
        [self.navigationController popToRootViewControllerAnimated:NO];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark - UIGestureRecognizerDelegate
//这个方法是在手势将要激活前调用：返回YES允许右滑手势的激活，返回NO不允许右滑手势的激活
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    NSString *changeAddress = [localSave objectForKey:@"changeAddressKey"];
    
    if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer) {
        //屏蔽调用rootViewController的滑动返回手势，避免右滑返回手势引起死机问题
        if ([changeAddress isEqualToString:@"Y"] || self.navigationController.viewControllers.count <= 2 || self.navigationController.visibleViewController == [self.navigationController.viewControllers objectAtIndex:0]) {
            return NO;
        }
    }
    return YES;
}


- (void)deleteWithAdressID:(NSString *)ID {
    [[HttpClient sharedInstance] postURL:@"http://er.ysbjob.com/v1/company/delete" parameter:@{@"id":ID} success:^(id  _Nonnull responseObject) {
        
        NSNumber *code = responseObject[@"status_code"];
        if ([code intValue] == 200) {
            
            [self loadData];
            
            NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
            [localSave setObject:@"Y" forKey:@"changeAddressKey"];
            [localSave synchronize];
            //删除地址后要回到首页置空选择的地址
            [[NSNotificationCenter defaultCenter] postNotificationName:@"deleteAddressNotification" object:nil];
            
        }
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}



@end




@implementation YSBCompanyMsgCell


- (IBAction)editClick:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(editMsg:)]) {
        
        NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
        [localSave setObject:@"Y" forKey:@"changeAddressKey"];
        [localSave synchronize];
        
        [_delegate editMsg:sender];
    }
}


@end



@implementation YSBTextFieldCell


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (string.length == 0) return YES;
    
    NSInteger length = textField.text.length;
    
    if (self.maxLength > 0) {
        if (length >= self.maxLength) {
            return NO;
        }
    }
    
    return YES;
}


@end



@implementation YSBChatRoomWebSocketManager


- (void)webSocketDidOpen:(SRWebSocket *)webSocket{
    NSLog(@"已经连接,开启心跳");

    NSDictionary *dict = @{
        @"action":@(1),
    };

    [self sendDataToServer:dict];
    
    self.isConnect = YES;
    self.socketStatus = 1;
    [self initHeartBeat];//开始心跳
    
    [self performSelector:@selector(delayShowTabBarItemBadgeValueMethod) withObject:nil afterDelay:1];
    
}

- (void)delayShowTabBarItemBadgeValueMethod {
    NSUserDefaults *localSave = [NSUserDefaults standardUserDefaults];
    //读取在数客服消息条数
    NSString *unread_msg_num = [localSave objectForKey:@"customerMsgNumKey"];
    unread_msg_num = [YSBCommonTool isBlankString:unread_msg_num] ? @"0" : unread_msg_num;
            
    UITabBarController *root = [YSBCommonTool getCurrentVC].tabBarController; // 当前的viewController
    UITabBarItem *tabBarItem = [UITabBarItem new];
    
    if (unread_msg_num.integerValue > 0) {
        
        if (root.tabBar.items.count > 2) {
            tabBarItem = root.tabBar.items[2];// 拿到需要设置角标的tabarItem
            [tabBarItem setBadgeValue:@"客服+"];
        }
        
    }else{
        [tabBarItem setBadgeValue:nil];// 隐藏角标
    }
    
}



@end



@implementation YSBMessageListHomeViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.tabBarItem.badgeValue = nil;

    [self requestData];
    
}


@end
